<template>
  <div class="dashboard-container">
    <div class="app-container">
      <!-- 靠右的按钮 -->
      <page-tools>
        <template v-slot:after>
          <el-button type="primary" size="small" @click="addRights('0', 1)"
            >添加权限</el-button
          >
        </template>
      </page-tools>
      <!-- 表格 -->
      <el-table border :data="list" row-key="id">
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="id" label="id" />
        <el-table-column prop="code" align="center" label="标识" />
        <el-table-column prop="description" align="center" label="描述" />
        <el-table-column align="center" label="操作">
          <template slot-scope="{ row }">
            <el-button
              v-if="row.type == 1"
              type="text"
              @click="addRights(row.id, 2)"
              >添加</el-button
            >
            <el-button type="text" @click="editHandle(row.id)">编辑</el-button>
            <el-button type="text" @click="del(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 对话框 -->
      <el-dialog :title="title" :visible.sync="dialogFlag" @close="close">
        <el-form :model="form" :rules="rules" ref="formRef">
          <el-form-item label="权限名称" label-width="100px" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限标识" label-width="100px" prop="code">
            <el-input v-model="form.code" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="权限描述" label-width="100px">
            <el-input v-model="form.description" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="开启" label-width="100px">
            <el-switch
              v-model="form.enVisible"
              active-color="#13ce66"
              inactive-color="#ccc"
            >
            </el-switch>

            =={{ form.enVisible }}
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFlag = false">取 消</el-button>
          <el-button type="primary" @click="add">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  permissionListApi,
  addPermissionApi,
  getPermissionDetailApi,
  updatePermissionApi,
  delPermissionApi
} from "@/api/permission";
import { tranListToTreeData } from "@/utils";
export default {
  name: "",
  data() {
    return {
      title: "新增权限点",
      list: [],
      dialogFlag: false,
      form: {
        name: "", // 名称
        code: "", // 标识
        description: "", // 描述
        type: "", // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: "", // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: "0" // 开启
      },
      rules: {
        name: [{ required: true, message: "请输入权限名称", trigger: "blur" }],
        code: [{ required: true, message: "请输入权限标识", trigger: "blur" }]
      }
    };
  },
  created() {
    console.log(this, 66666);
    this.getPermissionList();
  },
  methods: {
    async getPermissionList() {
      this.list = tranListToTreeData(await permissionListApi(), "0");
      // console.log(res, 22222);
    },

    /* 点击添加按钮 */
    addRights(pid, type) {
      this.title = "添加权限点";
      this.dialogFlag = true;
      //   console.log(pid, type, 77777);
      this.form.pid = pid;
      this.form.type = type;
    },
    /* 对话框中的确认按钮 */
    add() {
      //console.log(this.form, 8888);
      /*
      + 二次校验
      + 发送添加权限的请求
       */

      //   if (this.title == "编辑权限点") {
      //     this.$refs["formRef"].validate(async valide => {
      //       if (valide) {
      //         //  发送请求
      //         this.form.enVisible = this.form.enVisible ? "1" : "0";
      //         await updatePermissionApi(this.form);
      //         //  成功后：关闭对话框 + 刷新数据+ 表单置空
      //         this.close();
      //       }
      //     });
      //   } else {
      //     //  新增  添加
      //     this.$refs["formRef"].validate(async valide => {
      //       if (valide) {
      //         //  发送请求
      //         this.form.enVisible = this.form.enVisible ? "1" : "0";
      //         await addPermissionApi(this.form);
      //         //  成功后：关闭对话框 + 刷新数据+ 表单置空
      //         this.close();
      //       }
      //     });
      //   }

      this.$refs["formRef"].validate(async valide => {
        if (valide) {
          this.form.enVisible = this.form.enVisible ? "1" : "0";
          if (this.title == "编辑权限点") {
            await updatePermissionApi(this.form);
          } else {
            await addPermissionApi(this.form);
          }
          //  发送请求
          //  成功后：关闭对话框 + 刷新数据+ 表单置空
          this.close();
        }
      });
    },

    //  关闭对话框触发的函数
    close() {
      this.dialogFlag = false;
      this.getPermissionList();
      this.$refs["formRef"].resetFields();
      this.form = {
        name: "", // 名称
        code: "", // 标识
        description: "", // 描述
        type: "", // 类型 该类型 不需要显示 因为点击添加的时候已经知道类型了
        pid: "", // 因为做的是树 需要知道添加到哪个节点下了
        enVisible: "0" // 开启
      };
    },

    /* 点击编辑按钮 */
    async editHandle(id) {
      this.title = "编辑权限点";
      this.dialogFlag = true;

      /*
      + 显示对话框  跟添加权限是一个对话框
      + 显示内容到对话框中     需要编辑的内容
       */

      this.form = await getPermissionDetailApi(id);
      //console.log(res, 667788);
      this.form.enVisible = this.form.enVisible == "1" ? true : false;
    },

    /* 点击删除 */
    del(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          //  点击的是确认按钮
          await delPermissionApi(id);
          this.getPermissionList();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  }
};
</script>
<style lang="less" scoped></style>
